<?php
namespace Swiftx\Component\Router;
use Swiftx\Interfaces\Router\Response as RequestInterface;

class Response implements RequestInterface{

    /**
     * 自动刷新
     * @var bool
     */
    protected $autoFlush = true;

    /**
     * 输出暂存
     * @var string
     */
    protected $storage = '';

    /**
     * 设置自动刷新缓冲区
     * @param bool $config
     * @return void
     */
    public function setAutoFlush(bool $config){
        $this->autoFlush = $config;
    }

    /**
     * 刷新缓存区
     * @return void
     */
    public function flush(){
        echo $this->storage;
        $this->clearBuffer();
    }

    /**
     * 获取输出缓冲区内容
     * @return string
     */
    public function getContent():string{
        return $this->storage;
    }

    /**
     * 清理缓冲区内容
     * @return void
     */
    public function clearBuffer(){
        $this->storage = '';
    }

    /**
     * 往缓冲区输出数据
     * @param string $data
     * @return void
     */
    public function echo(string $data){
        if($this->autoFlush) echo $data;
        else $this->storage .= $data;
    }

}